var app = getApp()
var webhost = app.globalData.webhost;
var util = require('../../utils/util.js');
var utilDate = require('../../utils/date.js');

Page({
  data: {
    index: 0,
    dateStart: '出车日期起',
    dateEnd: '止',
    status: '',
    statusList: ['全部'],
    statusCodeList: [''],
    driverAccount: '',
    sessionid: '',
    pageNumber: 1,
    pageSize: 5,
    dataList: [],
    loading: 0
  },

  bindStartChange: function(e) {
    var date = e.detail.value;
    this.setData({
      dateStart: date,
      dataList: [],
      pageNumber: 1,
      loading: 0,
    })
    this.queryExcetionEvent()
  },

  bindEndChange: function(e) {
    var that = this;
    var date = e.detail.value;
    that.setData({
      dateEnd: date,
      dataList: [],
      pageNumber: 1,
      loading: 0,
    })
    this.queryExcetionEvent()
  },

  bindStatusChange: function(e) {
    var that = this;
    var index = e.detail.value;
    var status = that.data.statusCodeList[index];
    that.setData({
      index: index,
      status: status,
      dataList: [],
      pageNumber: 1,
      loading: 0,
    })
    this.queryExcetionEvent()
  },

  queryExcetionEvent: function() {
    var that = this;
    var myDate = new Date();
    var myDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);
    var date = utilDate.Format('yyyy-MM-dd', myDate);
    var date2 = utilDate.Format('yyyy-MM-dd', myDate2);
    var dateStart = that.data.dateStart == '出车日期起' ? date2 : that.data.dateStart;
    var dateEnd = that.data.dateEnd == '止' ? date : that.data.dateEnd;
    var status = that.data.status;
    var pageNumber = that.data.pageNumber;
    var pageSize = that.data.pageSize;
    var parameters = [{
      String: {
        driverAccount: app.globalData.driverAccount,
        sessionid: app.globalData.sessionid,
        status: status,
        dateStart: dateStart,
        dateEnd: dateEnd,
        pageNumber: pageNumber,
        pageSize: pageSize
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'queryExcetionEvent', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(parameters)
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        var code = res.data.code;
        if (code == '000000') {
          var dataList = res.data.dataList;
          if (dataList.length > 0) {
            
            for (var i = 0; i < dataList.length; i++) {
              if (dataList[i].exceptionFeedbackDate) {
                dataList[i].exceptionFeedbackDate = dataList[i].exceptionFeedbackDate.substring(0, 10)
              }
              if (dataList[i].n1) {
                dataList[i].n1 = dataList[i].n1.substring(0, 10)
              }
              if (dataList[i].n6) {
                dataList[i].n6 = dataList[i].n6.substring(0, 10)
              }
              if (dataList[i].n8){
                dataList[i].n8 = dataList[i].n8.substring(0,10)
              }
              if (dataList[i].n11) {
                dataList[i].n11 = dataList[i].n11.substring(0, 10)
              }
            }
            var newList = util.number_in_objOfList_toF2(dataList);
            that.setData({
              dataList: that.data.dataList.concat(dataList),
              loading: 0,
            })
          } else {
            that.setData({
              loading: 2,
            })
          }

        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            confirmColor: '#118EDE',
            content: res.data.msg,
            success: function(res) {
              if (code == '003000') {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '../login/login'
                  })
                }
              }
            }
          })
        }
      }
    })
  },

  onLoad: function(options) {
    var that = this;

    var codeList = [{
      String: {
        codeListKey: 'tsp2_abnormalStatus'
      }
    }];
    wx.request({
      url: webhost,
      data: {
        eap_username: "driver",
        eap_password: "Driver2018",
        boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
        methodName: 'getCodeList', //调用的具体方法
        returnType: "json", //返回参数类型
        parameters: JSON.stringify(codeList)

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        var code = res.data.code;
        if (code == '000000') {
          var index = that.data.index;
          var statusList = [];
          var statusCodeList = [];
          for (var i = 0; i < res.data.codeList.length; i++) {
            statusList[i] = res.data.codeList[i].codeName;
            statusCodeList[i] = res.data.codeList[i].codeValue;
          }
          that.setData({
            statusList: that.data.statusList.concat(statusList),
            statusCodeList: that.data.statusCodeList.concat(statusCodeList),
            status: ''
          })
          that.queryExcetionEvent();
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            confirmColor: '#118EDE',
            content: res.data.msg,
            success: function(res) {
              if (res.confirm) {}
            }
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '提示',
          showCancel: false,
          confirmColor: '#118EDE',
          content: res.data.msg,
          success: function(res) {
            if (res.confirm) {}
          }
        })
      }
    })


  },

  onReady: function() {

  },

  onShow: function() {

  },

  onPullDownRefresh() {
    var that = this;
    setTimeout(function() {
      that.setData({
        pageNumber: 1,
        dataList: []
      })
      that.queryExcetionEvent();
      wx.stopPullDownRefresh()
    }, 2000)
  },

  onReachBottom() {
    var that = this;
    if (that.data.loading == 0) {
      that.setData({
        loading: true,
        pageNumber: that.data.pageNumber + 1
      })
      setTimeout(() => {
        that.queryExcetionEvent();
      }, 300);
    }
  },


  onShareAppMessage: function() {

  }
})